<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单id" prop="receiptid">
      <el-input v-model="dataForm.receiptid" placeholder="订单id"></el-input>
    </el-form-item>
    <el-form-item label="食品id" prop="goodid">
      <el-input v-model="dataForm.goodid" placeholder="食品id"></el-input>
    </el-form-item>
    <el-form-item label="食品名称" prop="goodname">
      <el-input v-model="dataForm.goodname" placeholder="食品名称"></el-input>
    </el-form-item>
    <el-form-item label="单价" prop="price">
      <el-input v-model="dataForm.price" placeholder="单价"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="num">
      <el-input v-model="dataForm.num" placeholder="数量"></el-input>
    </el-form-item>
    <el-form-item label="合计金额" prop="totalmoney">
      <el-input v-model="dataForm.totalmoney" placeholder="合计金额"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="subable" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        subable: false,
        visible: false,
        dataForm: {
          id: 0,
          receiptid: '',
          goodid: '',
          goodname: '',
          price: '',
          num: '',
          totalmoney: ''
        },
        dataRule: {
          receiptid: [
            { required: true, message: '订单id不能为空', trigger: 'blur' }
          ],
          goodid: [
            { required: true, message: '食品id不能为空', trigger: 'blur' }
          ],
          goodname: [
            { required: true, message: '食品名称不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '单价不能为空', trigger: 'blur' }
          ],
          num: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          totalmoney: [
            { required: true, message: '合计金额不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.subable = false
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/goodrecepititem/goodrecepititem/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.receiptid = data.goodrecepititem.receiptid
                this.dataForm.goodid = data.goodrecepititem.goodid
                this.dataForm.goodname = data.goodrecepititem.goodname
                this.dataForm.price = data.goodrecepititem.price
                this.dataForm.num = data.goodrecepititem.num
                this.dataForm.totalmoney = data.goodrecepititem.totalmoney
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.subable = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/goodrecepititem/goodrecepititem/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'receiptid': this.dataForm.receiptid,
                'goodid': this.dataForm.goodid,
                'goodname': this.dataForm.goodname,
                'price': this.dataForm.price,
                'num': this.dataForm.num,
                'totalmoney': this.dataForm.totalmoney
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            this.subable = false
          }
        })
      }
    }
  }
</script>
